#include <gflags/gflags.h>

#include "../../../common/mysql.hpp"
#include "../../../common/mysql_chat_session_member.hpp"

DEFINE_bool(run_mode,false,"程序的运行模式，false： 调试 true： 发布");
DEFINE_string(log_file,"","发布模式下，向文件中输出，如果是调试模式下，模式是stdout");
DEFINE_int32(log_level,0,"发布模式下，用于指定的日志输出等级");

DEFINE_string(host,"127.0.0.1","这是mysql的地址");
DEFINE_int32(port,0,"这是mysql的端口");
DEFINE_string(db,"lch_im","数据库默认的库");
DEFINE_string(user,"root","这是用户名");
DEFINE_string(passward,"123456","这是密码");
DEFINE_string(cset,"utf8","这是mysql客户端字符集");
DEFINE_int32(max_pool,3,"这是mysql连接池最大连接数量");


void append_test(ChatSessionMemberTable& tb)
{
    ChatSessionMember csm1("会话ID1","用户ID1");
    tb.append_member(csm1);

    ChatSessionMember csm2("会话ID2","用户ID2");
    tb.append_member(csm2);

    ChatSessionMember csm3("会话ID1","用户ID2");
    tb.append_member(csm3);
}

void multi_append_test(ChatSessionMemberTable& tb)
{
    ChatSessionMember csm1("会话ID1","用户ID1");
    ChatSessionMember csm2("会话ID1","用户ID2");
    ChatSessionMember csm3("会话ID1","用户ID3");
    ChatSessionMember csm4("会话ID1","用户ID4");

    std::vector<ChatSessionMember> list({csm1,csm2,csm3,csm4});
    tb.append_member(list);    
}

void remove_test(ChatSessionMemberTable& tb)
{
    ChatSessionMember csm3("会话ID1","用户ID1");
    tb.remove_member(csm3);
}

void session_member(ChatSessionMemberTable& tb)
{
    auto res = tb.members("会话ID1");
    for(auto e : res ) {
        std::cout << e << std::endl;
    } 
}

void remove_all(ChatSessionMemberTable& tb)
{
    tb.remove_member("会话ID1");
}

int main(int argc,char* argv[])
{   
    google::ParseCommandLineFlags(&argc,&argv,true);
    init_logger(FLAGS_run_mode,FLAGS_log_file,FLAGS_log_level);

    auto db = ODBFactory::create(FLAGS_user,FLAGS_passward,FLAGS_db,FLAGS_host,FLAGS_cset,FLAGS_port,FLAGS_max_pool);
    ChatSessionMemberTable csmt(db);

//    append_test(csmt);
//    multi_append_test(csmt);
    remove_test(csmt);
    remove_all(csmt);
    session_member(csmt);

    return 0;
}